﻿
namespace StudentManegement.Models
{
    public class MockStudentRepository : IStudentRepository
    {
        private List<StudentModel> _students;

        public MockStudentRepository()
        {
            _students = new List<StudentModel>() 
            {
                new StudentModel() {Id = 1, Name ="张三", ClassName ="一年一班", EmailAddress="aaaaaa.com"},
                new StudentModel() {Id = 2, Name ="里斯", ClassName ="二年一班", EmailAddress="bbbbbb.com"},
                new StudentModel() {Id = 3, Name ="司命", ClassName ="三年一班", EmailAddress="cccccc.com"},
                new StudentModel() {Id = 4, Name ="瑶", ClassName ="四年一班", EmailAddress="ddddddd.com"}
            };
        }

        /// <summary>
        /// 添加学生
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public StudentModel CreatStudent(StudentModel student)
        {
            student.Id = _students.Max(x => x.Id) + 1;
            _students.Add(student);
            return student;
        }

        /// <summary>
        /// 根据ID删除学生
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public StudentModel DeleteStudent(int id)
        {
            StudentModel student = _students.FirstOrDefault(x => x.Id == id);
            if (student != null)
            {
                _students.Remove(student);
            }

            return student;
        }

        /// <summary>
        /// 查询所有学生信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IEnumerable<StudentModel> GetAllStudents()
        {
            return _students;
        }

        /// <summary>
        /// 根据ID查找学生信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public StudentModel GetStudentModel(int id)
        {
            return  _students.FirstOrDefault(item => item.Id == id);
        }

        /// <summary>
        /// 修改学生信息
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public StudentModel updateStudent(StudentModel updateStudent)
        {
            StudentModel studentModel = _students.FirstOrDefault(s => s.Id == updateStudent.Id);
            if (studentModel != null)
            {
                studentModel.Id = updateStudent.Id;
                studentModel.Name = updateStudent.Name;
                studentModel.EmailAddress = updateStudent.EmailAddress;
                studentModel.ClassName = updateStudent.ClassName;
            }

            return studentModel;
        }
    }
}
